Ext.define('js.project.engineeringSupervisionShowPage', {
    extend: 'Ext.grid.Panel',
    id:'engineeringSupervisionShowPage',
    requires: ['js.Util', 'js.ctrl.Common',],
    controller: 'Common',
    autoScroll : true,
    margin:'20 30 30 30',
    height:620,
    bodyStyle: {
        backgroundColor: '#eee',
    },
    initComponent: function () {
        this.store = Ext.create('js.store.project.engineeringSupervisionListStore');
         this.store.getProxy().setExtraParam('pageNum', 1);
         this.store.load({
             callback:function () {
                 let me = Ext.getCmp('engineeringSupervisionShowPage');
                 if(me){
                     if(me.store.data.items.length>0){
                         new Page({
                             id: 'paginationSupplier',
                             pageTotal: me.store.data.items[0].data.pageTotal, //必填,总页数
                             pageAmount: 10,  //每页多少条
                             dataTotal: me.store.data.items[0].data.dataTotal, //总共多少条数据
                             curPage:parseInt(me.store.data.items[0].data.pageNum), //初始页码,不填默认为1
                             pageSize: 5, //分页个数,不填默认为5
                             showPageTotalFlag:true, //是否显示数据统计,不填默认不显示
                             showSkipInputFlag:true, //是否支持跳转,不填默认不显示
                             getPage: function (page) {
                                 //获取当前页数
                                 me.store.getProxy().setExtraParam('pageNum', page);
                                 me.store.reload();
                             }
                         })
                     }else{
                         new Page({
                             id: 'paginationSupplier',
                             pageTotal: 1, //必填,总页数
                             pageAmount: 10,  //每页多少条
                             dataTotal: 0, //总共多少条数据
                             pageSize: 5, //分页个数,不填默认为5
                             showPageTotalFlag:true, //是否显示数据统计,不填默认不显示
                             showSkipInputFlag:true, //是否支持跳转,不填默认不显示
                             getPage: function (page) {
                                 //获取当前页数
                                 me.store.getProxy().setExtraParam('pageNum', page);
                                 me.store.reload();
                             }
                         })
                     }
                 }
             }
         });
        this.columns = [
            {text: '工程编号', dataIndex: 'engineering_no', align: 'center', flex: 1,
                renderer: function (value, cellmeta, record, rowIndex, columnIndex, store) {//自定义列值组合
                    return `<div class='mutualAAA'><a herf="#">${value}</a></div>`;
                }},
            {text: '工程名称', dataIndex: 'engineering_name', align: 'center', flex: 1},
            {text: '开工日期', dataIndex: 'startDate', align: 'center', flex: 1,},
            {text: '结构类型', dataIndex: 'structure_type', align: 'center', flex: 1,},
            {text: '建筑用途', dataIndex: 'building_use', align: 'center', flex: 1,},
            {text: '工程地点区域', dataIndex: 'region_name', align: 'center', flex: 1,},
          /*  {text: '责任监督人', dataIndex: 'principal_id', align: 'center', flex: 1,},*/
            {text: '工程状态', dataIndex: 'status', align: 'center', flex: 1,
                renderer: function (value, cellmeta, record, rowIndex, columnIndex, store) {//自定义列值组合
                    return `<div class='mutualAA'><a herf="#">${value}</a></div>`;
                }
            }
        ];
        this.dockedItems = [
            {
                dock: 'top',
                xtype: 'toolbar',
                border:'none',
                margin:'0 0 0 0',
                items: [{
                    xtype: 'panel',
                    ui: 'threshold1',
                    bodyStyle:{
                        background:'#eee'
                    },
                    width:'100%',
                    margin:'0 0 0 -21px',
                    height: 95,
                    border: false,
                    layout: 'vbox',
                    id:'engineeringSupervisionButtonGroup',
                    items:[
                        {
                            xtype:'panel',
                            height:45,
                            width:'100%',
                            layout: 'hbox',
                            bodyStyle:{
                                background:'#eee'
                            },
                            items:[
                             /*   {
                                    xtype: 'combobox',
                                    height: 36,
                                    width:'25%',
                                    emptyText: '项目筛选',
                                    hidden : true,
                                    margin:'0 58% 0 29%',
                                    triggerAction: 'all',
                                    editable: false,
                                    displayField: 'dataType',
                                    valueField: 'value',
                                    store:  Ext.create('js.store.wide.projectQueryOneStore'),
                                    /!* store: this.store,
                                     listeners: {
                                         change: function (com, newValue, oldValue, eOpts) {//企业ID
                                             comboStore.load({params: {'conditionValue': newValue, 'args': 1,},
                                                 callback : function(record, options, success) {
                                                     if(Ext.getCmp('thresholdDeviceC') !== null)
                                                         Ext.getCmp('thresholdDeviceC').setValue(record[0].data.value);
                                                 }}
                                             );
                                         }
                                     }*!/
                                },*/{
                                    xtype: 'combobox',
                                    emptyText: '建筑用途',
                                    triggerAction: 'all',
                                    id:'jzyt',
                                    height: 30,
                                    width: '25%',
                                    margin:'0 58% 10 29%',
                                    labelSeparator: '',
                                    labelWidth: 70,
                                    editable: false,
                                    displayField: 'dataType',
                                    valueField: 'value',
                                    store:  Ext.create('js.store.wide.projectConstructionApplications',{
                                        proxy: {
                                            extraParams: {
                                                type: 0,
                                            },
                                        }
                                    }),
                                    listeners: {
                                    change: function (com, newValue, oldValue, eOpts) {//企业ID
                                        let me = Ext.getCmp('engineeringSupervisionShowPage');
                                        me.store.getProxy().setExtraParam('buildingUse', newValue);
                                        me.store.reload();
                                    }
                                }
                                },{
                                    xtype: 'combobox',
                                    height: 30,
                                    emptyText: '工程状态',
                                    margin:'0 58% 0 29%',
                                    id:'gczt',
                                    width: '25%',
                                    labelSeparator: '',
                                    labelWidth: 70,
                                    editable: false,
                                    displayField: 'dataType',
                                    valueField: 'value',
                                    store:  Ext.create('js.store.wide.projectStateA'),
                                    listeners: {
                                        change: function (com, newValue, oldValue, eOpts) {//企业ID
                                            let me = Ext.getCmp('engineeringSupervisionShowPage');
                                            me.store.getProxy().setExtraParam('engineeringStatus', newValue);
                                            me.store.reload();
                                        }
                                    }
                                },{
                                    xtype: 'textfield',
                                    height:30,
                                    width:'25%',
                                    id:'ddmc',
                                    margin:'0 40% 0 29% ',
                                    value: '工程名称,地点',
                                    listeners:{
                                        focus: function(){
                                            Ext.getCmp('ddmc').setValue('')
                                        },
                                        specialkey:function(){
                                            let value = Ext.getCmp('ddmc').getValue();
                                            let me = Ext.getCmp('engineeringSupervisionShowPage');
                                            me.store.getProxy().setExtraParam('searchValue', value);
                                            me.store.reload();

                                        }
                                    }
                                }
                            ]},
                        {
                            xtype:'panel',
                            height:40,
                            width:'100%',
                            layout: 'hbox',
                            bodyStyle:{
                                background:'#eee'
                            },
                            items:[
                                {
                                    xtype : "button",
                                    text:'项目合并',
                                    cls:'button-Intelligent-sensor',
                                    style:"cursor:pointer",
                                    margin:'0 5% 0 29%',
                                    width: '13%',
                                    hidden : true,
                                    handler: this.projectMerger,
                                    action_type: 'enterpriseAdd',
                                    height: 40,
                                },{
                                    xtype : "button",
                                    text:'添加',
                                    cls:'button-Intelligent-sensor',
                                    style:"cursor:pointer",
                                    margin:'0 5% 0 29%',
                                    hidden: util.hidden('engineering-btn-add'),
                                    width: '9.5%',
                                    scope: this,
                                    handler: this.projectMergerAdd,
                                    height: 32,
                                    action_type: 'engineeringAdd',
                                },{
                                    xtype : "button",
                                    text:'编辑',
                                    cls:'button-Intelligent-sensor',
                                    style:"cursor:pointer",
                                    hidden: util.hidden('engineering-btn-edit'),
                                    margin:'0 5% 0 29%',
                                    action_type: 'engineeringUpdate',
                                    handler: this.projectMergerAdd,
                                    width: '9.5%',
                                    height: 32,
                                },{
                                    xtype : "button",
                                    text:'删除',
                                    hidden: util.hidden('engineering-btn-delete'),
                                    cls:'button-Intelligent-sensorA',
                                    style:"cursor:pointer",
                                    margin:'0 5% 0 29%',
                                    width: '9.5%',
                                    handler: this.projectMergerDel,
                                    height: 32,
                                },{
                                    xtype : "button",
                                    text:'刷新',
                                    cls:'button-Intelligent-sensor',
                                    style:"cursor:pointer",
                                    margin:'0 5% 0 29%',
                                    handler: this.refresh,
                                    scope: this,
                                    width: '9.5%',
                                    height: 32,
                                },{
                                    xtype : "button",
                                    text:'克隆',
                                    cls:'button-Intelligent-sensor',
                                    style:"cursor:pointer",
                                    margin:'0 5% 0 29%',
                                    handler: this.copy,
                                    scope: this,
                                    width: '9.5%',
                                    height: 32,
                                },{
                                    xtype: 'component',
                                    html: `<span style="font-size:14px;">开工日期：</span>`,
                                    margin:'9 5% 0 80%',
                                    width:'8%',
                                    height: '100%'

                                },{
                                    xtype: 'datefield',
                                    name: 'start_time',
                                    id:'kgrq',
                                    width:'20%',
                                    height: 30,
                                    editable:false,
                                    listeners: {
                                        change: function (com, newValue, oldValue, eOpts) {//企业ID
                                            let startDateB= Ext.getCmp('jsrq').getValue();
                                            let me = Ext.getCmp('engineeringSupervisionShowPage');
                                            Ext.getCmp('jsrq').setMinValue(new Date(newValue));
                                            me.store.getProxy().setExtraParam('startDateB', startDateB);
                                            me.store.getProxy().setExtraParam('startDateA', newValue);
                                            me.store.reload();
                                        }
                                    }
                                },{
                                    xtype: 'component',
                                    html: `<span style="color:#2672d9;font-weight: bold;"> ---- </span>`,
                                    margin:'9 -10 0 5',
                                    width:'4%',
                                    height: '100%',
                                },{
                                    xtype: 'datefield',
                                    name: 'end_time',
                                    id:'jsrq',
                                    width:'20%',
                                    height: 30,
                                    editable:false,
                                    listeners: {
                                        change: function (com, newValue, oldValue, eOpts) {//企业ID
                                            let startDateA= Ext.getCmp('kgrq').getValue();

                                            let me = Ext.getCmp('engineeringSupervisionShowPage');
                                            me.store.getProxy().setExtraParam('startDateB', newValue);
                                            me.store.getProxy().setExtraParam('startDateA', startDateA);
                                            me.store.reload();
                                        }
                                    }
                                }
                            ]}
                    ]

                }]

            },{
                xtype: 'panel',
                html:'<div id="paginationSupplier" class="pagination"></div>',
                dock: 'bottom',
                height:50,
                margin:'-14 0 0 0',
                displayInfo: true,
            }]
        this.selModel = Ext.create('Ext.selection.CheckboxModel', {mode: 'MULTI', text: '全选'});
        this.on('cellclick', this.cellclick);
        this.callParent(arguments);
    },
    cellclick: function (td, cellIndex, record, tr, rowIndex, e, eOpts) {
        if (record === 1 || record === 7) {
            //连结后台查询
            Ext.Ajax.request({
                url: '/engInformationRoutes',
                method: "POST",
                async: true,  //异步调用AJAX
                params: {
                    action_type: 'engInformationQuery',
                    engineering_id:tr.data.engineering_id
                },
                success: function (response, opts) {
                    let result = Ext.decode(response.responseText);
                    let params = {
                        engineering_id: tr.data.engineering_id,
                        engineering_no: tr.data.engineering_no,
                        engineering_name: tr.data.engineering_name,
                        num:2,

                        //开工日期
                        startDate:result.data.startDate,
                        //责任监督员
                        principal_name:result.data.principal_name,
                        //上
                        aboveGround_floor:result.data.aboveGround_floor,
                        //首次入场日期
                        admission_construction_date:result.data.admission_construction_date,
                        //下
                        underGround_floor:result.data.underGround_floor,
                        //监督员
                        user_name:result.data.user_name,
                        //总面积
                        engineering_area:result.data.engineering_area,
                        //入场工程状态
                        admission_construction_status:result.data.admission_construction_status,
                        //详细地址
                        engineering_address:result.data.engineering_address,
                        //建筑用途
                        building_use:result.data.building_use,
                        //监督注册证编号
                        supervisory_registration_certificate:result.data.supervisory_registration_certificate,
                        //规划证号
                        planning_certificate:result.data.planning_certificate,
                        //图审编号
                        drawing_number:result.data.drawing_number,
                        //施工许可证号
                        construction_permit:result.data.construction_permit,
                        //建设单位
                        construction_unit_employer_id:result.data.construction_unit_employer_id,
                        //设计单位
                        design_unit_id:result.data.design_unit_id,
                        //施工单位
                        construction_unit_contractor_id:result.data.construction_unit_contractor_id,
                        //监理单位
                        supervisory_unit_id:result.data.supervisory_unit_id,
                        //勘察单位
                        survey_unit_id:result.data.survey_unit_id,
                        //图审单位
                        drawing_unit_id:result.data.drawing_unit_id,
                        //责任监督员id
                        principal_id:result.data.principal_id,
                        //监督员id
                        userIds:result.data.userIds,
                    };
                    util.sysPanelAdd('js.information.InformationListMain', params);


                },
                failure: function () {
                    Ext.toast(`获取异常报警数据失败：${response.status}`);
                }  // 请求失败的回调函数
            });
        }
    },
    projectMerger: function (btn) {
        let params = {
            action_type: btn.action_type
        };
        let btnCancel = Ext.create('Ext.button.Button', {
            cls: 'btnCancel',
            text: `取消`,
            scale: 'medium',
            width:140,
            height:42,
            margin:'0 0 45 200',
            handler: function (btn) {
                btn.up('window').close();
            },
            scope: this

        });
        let btnSubmit = Ext.create('Ext.button.Button', {
            cls:'btnSubmit',
            border:false,
            text: `保存`,
            margin: '0 0 45 30',
            width:140,
            height:42,
            scale: 'medium',
            handler: function (btn) {
                btn.up('window').close();
            },
            scope: this
        });
        let formPanel = Ext.create('js.project.ProjectMergerPage', {
            hidenField: params,
            id: 'CompaniesAddPage'
        });
        let win = util.window(formPanel, {title: '项目合并',width:'600',height:'438',bodyStyle: {backgroundColor: '#253746'}, buttons: ['->', btnCancel, btnSubmit, '->']});
        btnSubmit.on('click', formPanel.submit, formPanel);


        win.show();

        formPanel.on('actioncomplete', () => {
            this.getStore().reload();
            win.close();
        });
    },
    projectMergerAdd: function (btn) {
        let params = {
            action_type: btn.action_type
        };
        let btnCancel = Ext.create('Ext.button.Button', {
            cls: 'btnCancel',
            text: `取消`,
            scale: 'medium',
            width:140,
            height:42,
            margin:'0 0 10 200',
            handler: function (btn) {
                btn.up('window').close();
            },
            scope: this

        });
        let btnSubmit = Ext.create('Ext.button.Button', {
            cls:'btnSubmit',
            border:false,
            text: `保存`,
            margin: '0 0 10 30',
            width:140,
            height:42,
            scale: 'medium',
            handler: function (btn) {
                   let engineering_id=Ext.getCmp('eng_id').getValue();
                   let eng_name=Ext.getCmp('eng_name').getValue();
                   let eng_startDate=Ext.getCmp('eng_startDate').getValue();
                   let eng_structure_type=Ext.getCmp('eng_structure_type').getValue();
                   let eng_building_use=Ext.getCmp('eng_building_use').getValue();
                   let eng_region_name=Ext.getCmp('eng_region_name').getValue();
                   let eng_status=Ext.getCmp('eng_status').getValue();
                if(eng_name===''||eng_startDate===''||eng_structure_type===null||eng_building_use===null||eng_region_name===null||eng_status===null){
                    Ext.toast('请将信息填写完整');
                }else {
                    let panel =Ext.getCmp('engineeringSupervisionShowPage')
                    panel.mask('正在加载数据请稍后');
                    util.post('/engineeringRoutes', {
                        action_type: params.action_type,
                        engineering_id: engineering_id,
                        eng_name: eng_name,
                        eng_startDate: eng_startDate,
                        eng_structure_type: eng_structure_type,
                        eng_building_use: eng_building_use,
                        eng_region_name: eng_region_name,
                        eng_status: eng_status,
                    }).then(function (response) {
                        panel.unmask();
                        let obj = Ext.decode(response.responseText);
                        if (obj.success) {
                            Ext.toast('保存成功');
                            let me = Ext.getCmp('engineeringSupervisionShowPage');
                            me.store.reload();
                            btn.up('window').close();
                        } else {
                            Ext.toast(`<div style="font-size:28px;">保存失败,${obj.message}</div>`);
                        }
                    });
                }
            },
            scope: this
        });
        let formPanel = Ext.create('js.project.ProjectMergerAddPage', {
            hidenField: params,
            id: 'CompaniesAddPage',
        });
        let projectMergertitle='';
        if(btn.action_type=='engineeringUpdate'){
            projectMergertitle='编辑项目'
        }else{
            projectMergertitle='添加项目'
        }
        let win = util.window(formPanel, {title:projectMergertitle ,width:'40%',height:'53%', buttons: ['->', btnCancel, btnSubmit, '->']});
        let resultSelection = Ext.getCmp('engineeringSupervisionShowPage').getSelection();
        if(btn.action_type === 'engineeringUpdate'){
            let ids = Ext.Array.map(resultSelection, (item) => {
                return item.get('engineering_id')
            });
            if(ids.length>1){
                Ext.toast('请选择单个工程')
            }else if(ids.length ===0){
                Ext.toast('请选择需要修改的工程')
            }else{
                util.post('engineeringRoutes', {
                    action_type: 'engineeringQuery',
                    engineering_id: ids[0],
                }).then(function (response) {
                    let obj = Ext.decode(response.responseText);
                    win.show();
                    Ext.getCmp('eng_id').setValue(obj.data.engineering_id);
                    Ext.getCmp('eng_name').setValue(obj.data.engineering_name);
                    Ext.getCmp('eng_startDate').setValue(obj.data.startDate);
                    Ext.getCmp('eng_structure_type').setValue(obj.data.structure_id);
                    Ext.getCmp('eng_building_use').setValue(obj.data.building_id);
                    Ext.getCmp('eng_region_name').setValue(obj.data.region_id);
                    Ext.getCmp('eng_status').setValue(obj.data.status);

                })
            }

        }else{
            win.show();
        }

        if(btn.action_type === '')
            formPanel.on('actioncomplete', () => {
                this.getStore().reload();
                win.close();
            });
    },
    //克隆
    copy:function(btn){
        let btnCancel = Ext.create('Ext.button.Button', {
            cls: 'btnCancel',
            text: `取消`,
            scale: 'medium',
            width:140,
            height:42,
            margin:'0 0 10 200',
            handler: function (btn) {
                btn.up('window').close();
            },
            scope: this

        });
        let btnSubmit = Ext.create('Ext.button.Button', {
            cls:'btnSubmit',
            border:false,
            text: `保存`,
            margin: '0 0 10 30',
            width:140,
            height:42,
            scale: 'medium',
            handler: function (btn) {
                let engineering_id=Ext.getCmp('engineering_no').getValue();
                //项目名
                let eng_name=Ext.getCmp('eng_name').getValue();
                //地上
                let aboveGround_floorA=Ext.getCmp('aboveGround_floor').value;
                //地下
                let underGround_floorA=Ext.getCmp('underGround_floor').value;
                //总面积
                let engineering_area=Ext.getCmp('engineering_area').getValue();
                if(eng_name===''){
                    Ext.toast('请将信息填写完整');
                }else {
                    let panel =Ext.getCmp('engineeringSupervisionShowPage')
                    panel.mask('正在加载数据请稍后');
                    util.post('/engineeringRoutes', {//路径
                        action_type: 'engineeringClone',//action
                        engineering_id:engineering_id,
                        eng_name: eng_name,
                        aboveGround_floorA: aboveGround_floorA,
                        underGround_floorA: underGround_floorA,
                        engineering_area: engineering_area,
                    }).then(function (response) {
                        panel.unmask();
                        let obj = Ext.decode(response.responseText);
                        if (obj.success) {
                            Ext.toast('克隆成功');
                            let me = Ext.getCmp('engineeringSupervisionShowPage');
                            me.store.reload();
                            btn.up('window').close();
                        } else {
                            Ext.toast(`<div style="font-size:28px;">保存失败,${obj.message}</div>`);
                        }
                    });
                }
            },
            scope: this
        });
        let resultSelection = Ext.getCmp('engineeringSupervisionShowPage').getSelection();
        let ids = Ext.Array.map(resultSelection, (item) => {
            return item.get('engineering_id')
        });
        let formPanel = Ext.create('js.project.ProjectMergerCopyPage', {
            ids:ids
        });
        let win = util.window(formPanel, {title:"克隆项目" ,width:'40%',height:'30%', buttons: ['->', btnCancel, btnSubmit, '->']});


        if(ids.length>1){
            Ext.toast('请选择单个工程')
        }else if(ids.length ===0){
            Ext.toast('请选择需要修改的工程')
        }else{
            win.show();
        }
    },
    //删除
    deleteManager: function () {
        let resultSelection = this.getSelection();
        let ids = Ext.Array.map(resultSelection, (item) => {
            return item.get('engineering_id')
        });
        util.get('/engineeringRoutes', {
            action_type: 'engineeringDel',
            ids: ids.join(',')
        }).then(this.delSuccess, this.delFail);
    },
    projectMergerDel: function (btn) {
        let me = Ext.getCmp('engineeringSupervisionShowPage');
        let userSelection = me.getSelection();
        if (userSelection.length <= 0) {
            Ext.toast('请选择需要删除的项目');
        } else {
            util.deleteConfirm(function () {
                util.prompt(me.deleteManager, me);
            })
        }
    },
    delSuccess: function (response) {
        let obj = Ext.decode(response.responseText);
        if (obj.success) {
            Ext.toast('删除成功');
            Ext.getCmp('engineeringSupervisionShowPage').getStore().reload();
        } else {
            Ext.toast(obj.message);
        }
    },
    delFail: function (response) {
        Ext.toast(`<div style="font-size:28px">删除失败：${response.status}</div>`);
    },
    refresh: function (btn) {
        Ext.getCmp('jzyt').setValue('');
        Ext.getCmp('ddmc').setValue('');
        Ext.getCmp('gczt').setValue('');
        Ext.getCmp('kgrq').setValue('');
        Ext.getCmp('jsrq').setValue('');
        this.getStore().reload();
    }

});